﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Collections.Generic;
using System.Xml.Linq;

namespace PlayDeezGames.Common
{
    public class Descriptor : EntityBase
    {
        public class DescriptorFactory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new Descriptor(theParent, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new Descriptor(theParent);
            }
            private DescriptorFactory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.CommonFactories.DescriptorFactory, new Descriptor.DescriptorFactory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.CommonFactories.DescriptorFactory);
            }
        }

        private Dictionary<string, Property> properties = new Dictionary<string, Property>();

        public Dictionary<string, Property>.KeyCollection Properties
        {
            get
            {
                return properties.Keys;
            }
        }

        public Property this[string index]
        {
            get
            {
                if (properties.ContainsKey(index))
                {
                    return properties[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (properties.ContainsKey(index))
                {
                    properties[index].ParentEntity = null;
                    properties.Remove(index);
                }
                if (value != null)
                {
                    properties[index] = value;
                }
            }
        }


        public Descriptor(IEntity parent)
            : base(parent)
        {
        }
        public Descriptor(IEntity parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        protected override XElement OnToXElement()
        {
            XElement result = new XElement(Names.CommonXNames.Descriptor);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.CommonFactories.DescriptorFactory));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
        }

        public static Descriptor Create(IEntity theParent)
        {
            return EntityFactories.Create(theParent, Names.CommonFactories.DescriptorFactory) as Descriptor;
        }

        public static Descriptor Load(IEntity theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as Descriptor;
        }
    }
}
